<?php
class indexControl extends Control{
    public function index(){
    	$this->get_citys_view();
    	$this->house_hot_citys();
		$this->display();
    }
    //热门导航位置需要的城市
    private function house_hot_citys(){
    	$db = M("citys");
    	$citys = $db->field('cid,enname,cname,ishot,thumb')->where(" status = 1")->
    	limit(14)->all();
    	$this->assign("hot_citys",$citys);
    	$citys_img = array_rand($citys,12);
    	$result = array();
    	foreach($citys as $k=>$v){
    		if(in_array($k,$citys_img)){
    			$result[] = $citys[$k];
    		}
    	}
    	$this->assign('citys_img',$result);
    }
    //ajax 获得选中房屋的选项信息
    public function ajax_get_hot_city_info(){
    	C('debug',0);
    	$cid = $_POST['cid'];
    	$jingguan = $this->get_jingguan_view($cid);
    	$jingqu = $this->get_jingqu_view($cid);
    	$yangshi = $this->get_yangshi_view($cid);
    	$str = '';
    	$str = '<dl><dt>房型:</dt><dd>';
    	if(empty($yangshi)){
    		$str .= "<a target='_black'  href='#'>无</a>";
    	}else{
	    	foreach($yangshi as $k=>$v){
	    		$str .= "<a target='_black'  href='".__CONTROL__."/search/cid/".$cid."/s/0-".$v['yid']."-0-0-0-0'>".$v['yname']."</a>";
	    	}
    	}
    	$str .="</dd></dl><dl><dt>价格:</dt><dd>";
    	$str .="<a target='_black'  target='_black' href='".__CONTROL__."/search/cid/".$cid."/s/0-0-1-0-0-0'>￥200元以下</a>";
    	$str .="<a target='_black'  href='".__CONTROL__."/search/cid/".$cid."/s/0-0-2-0-0-0'>￥200元以下</a>";
    	$str .="<a target='_black'  href='".__CONTROL__."/search/cid/".$cid."/s/0-0-3-0-0-0'>￥200元以下</a>";
    	$str .="<a target='_black'  href='".__CONTROL__."/search/cid/".$cid."/s/0-0-4-0-0-0'>￥200元以下</a>";
    	$str .="<a target='_black'  href='".__CONTROL__."/search/cid/".$cid."/s/0-0-5-0-0-0'>￥200元以下</a>";
    	$str .="</dd></dl><dl><dt>价格:</dt><dd>";
    	if(empty($jingguan)){
    		$str .= "<a target='_black'  href='#'>无</a>";
    	}else{
	    	foreach($jingguan as $k=>$v){
	    		$str .= "<a target='_black'  href='".__CONTROL__."/search/cid/".$cid."/s/0-0-0-".$v['jgid']."-0-0'>".$v['jgname']."</a>";
	    	}
    	}
    	$str .="</dd></dl><dl><dt>景区:</dt><dd>";
    	if(empty($jingqu)){
    		$str .= "<a target='_black'  href='#'>无</a>";
    	}else{
	    	foreach($jingqu as $k=>$v){
	    		$str .= "<a target='_black'  href='".__CONTROL__."/search/cid/".$cid."/s/".$v['jid']."-0-0-0-0-0'>".$v['jname']."</a>";
	    	}
    	}
    	$str .="</dd></dl>";
    	echo $str;
    }
    /**
     * 搜索页面
     * 搜索页面的思路：
     *   	where()->根据house表和该搜索条件的关系决定
     *   	1.house 表和房屋样式表为多对多的关系。 需要使用 关联表来查询
     *   	2.house 表和该 房屋所在的小区是 多对一 的关系。这样直接加上where条件就行了。 
     */
    public function search(){
    	if(!isset($_GET['cid']) && !empty($_GET['cid'])){
    		$this->error('参数有误',__WEB__);
    	}else{
    		$cid = $_GET['cid'];
    	}
    	$_GET['s'] = isset($_GET['s']) && !empty($_GET['s'])?$_GET['s']:'0-0-0-0-0-0';
    	$_GET['pt'] = isset($_GET['pt']) && !empty($_GET['pt'])?$_GET['pt']:'0'; //配套设施
    	//这里开始处理开始和结束时间 还有按照 床数 加床 卧室数等等来作为 查询条件
        if(isset($_GET['cid_']) && !empty($_GET['cid_'])){
            if($_GET['cid'] != $_GET['cid_']){
                $this->error('参数错误,需要cid和cid_相同');
            }else{
                if(isset($_GET['tiaojian']) && isset($_GET['num'])){
                	$_GET[$_GET['tiaojian']] = $_GET["num"];
                	unset($_GET['tiaojian']);
                	unset($_GET['num']);
                }
            }
        }
		//P($_COOKIE);
    	$gets = $this->do_get($_GET); //向模版分配GET参数。 然后返回GET参数
    	$order_order = $this->get_where_order($gets); //处理下面条件中的 hot，面积，价格来排序的where条件
    	$options = $this->do_options($gets); //根据GET参数获取已经选择的条件
    	$this->do_options_title($options);
    	$this->assign("options",$options); // 分配  已经选择的 搜索条件
    	$hids = $this->get_hid($gets,$cid);  //获取符合搜索条件的房屋ID(多对多)
    	$db = V('house');
    	$db->view = array(
    			
    		);
    	$where_hid = $hids?" id in(".$hids.")":'';
    	$this->setCookie($_GET); //设置COOKIE
    	$this->get_yangshi_view($cid); //分配当前城市的 房屋样式信息
    	$this->get_xq_view($cid); //向模板分配当前查询城市的小区信息
    	$this->get_citys_view();//分配城市信息到模版
    	$this->get_cookieAndget($_GET); //分配cookie或者GET信息到模板
    	$this->get_jingqu_view($cid); //分配 当前城市景区信息到模板
    	$this->get_sheshi_view(); //分配房屋设施信息到模版
    	$this->get_jingguan_view($cid); //获得当前城市所有的景观
    	$this->get_ts_view($cid);
    	$where = $this->get_tiaojian($_GET); //条件
    	$where_xq = !empty($gets['xq'])?" qid = '".$gets['xq']."' ":''; //房屋的 小区 是 一对多的关系 。直接使用 where 条件查询就行了。
    	$where_jq = !empty($gets['jq'])?" jid = '".$gets['jq']."'":'';
    	$where_price = $this->get_price_where($gets['p']);
    	if(empty($where_hid)){
    		$houses = array();
    		$this->assign('page','');
    		$count = 0;
    	}else{
	    	$count = $db->where($where_hid)->where(" status = 1")->where($where_xq)->
    		where($where_price)->where($where_jq)->where($where['chuang'])->
    		where($where['woshi'])->where($where["goust"])->where($where['jiachuang'])->
    		where($where['date'])->order($order_order)->
    		count();
    		$page = new page($count,20);
    		$houses = $db->where($where_hid)->where(" status = 1")->where($where_xq)->
    		where($where_price)->where($where_jq)->where($where['chuang'])->
    		where($where['woshi'])->where($where["goust"])->where($where['jiachuang'])->
    		where($where['date'])->order($order_order)->
    		limit($page->limit())->all();
    		
    		$this->assign('page',$page->show(4));
    		
    	}
    	$this->assign("count",$count);
    	
    	$hosues = houseControl::do_geju($houses,2); //处理 房屋的格局 1室1厅1卫
    	$this->assign('houses',$hosues);
    	$this->display();
    }
    //处理下面条件中的 hot，面积，价格来排序的where条件
    private function get_where_order($gets){
    	$order = $gets['order'];
    	if($order == 'area'){
    		$where = " area desc";
    	}elseif($order == 'price'){
    		$where = " price desc";
    	}else{
    		$where = ' id desc';
    	}
    	return $where;
    }
    //获得options 放入到title里面去
    private function do_options_title($options){
    	$result = '';
    	if(!empty($options)){
    		foreach($options as $k=>$v){
    			$arr[] = $v['value'];
    		}
    		$result = implode(',',$arr);
    	}
    	$this->assign('options_title',$result);
    }
    /**
     * 通过$_GET 和$_COOKIE 来获取  where 值
     * @param Array  $_GET  get参数
     * @return Array  $where
     */
    private function get_tiaojian($_GET){
    	$where['chuang'] = isset($_GET['chuangCount']) && !empty($_GET['chuangCount'])?" chuang = '".$_GET['chuangCount']."' ":
    		(isset($_COOKIE['search']["chuangCount"]) && !empty($_COOKIE['search']["chuangCount"])?" chuang = '".$_COOKIE['search']["chuangCount"]."'":""); //床数
    	$where['woshi'] = isset($_GET['woshiCount']) && !empty($_GET['woshiCount'])?" woshi = '".$_GET['woshiCount']."'":
    		(isset($_COOKIE['search']["woshiCount"]) && !empty($_COOKIE['search']["woshiCount"])?" woshi = '".$_COOKIE['search']["woshiCount"]."'":"");  //卧室数
    	$where['goust'] = isset($_GET['guestCount']) && !empty($_GET['guestCount'])?" rezhu = '".$_GET['guestCount']."'":
    		(isset($_COOKIE['search']["guestCount"]) && !empty($_COOKIE['search']["guestCount"])?" rezhu = '".$_COOKIE['search']["guestCount"]."'":""); // 推荐入住人数
    	$where['jiachuang'] = isset($_GET['addBed']) && !empty($_GET['addBed'])?" jiachuang = '".$_GET['addBed']."'":
    		(isset($_COOKIE['search']["addBed"]) && !empty($_COOKIE['search']["addBed"])?" jiachuang = '".$_COOKIE['search']["addBed"]."'":""); //加床
    	$startdate = isset($_GET['startdate']) && !empty($_GET['startdate'])?strtotime($_GET['startdate']):
    		(isset($_COOKIE['search']["startdate"]) && !empty($_COOKIE['search']["startdate"])?$_COOKIE['search']["startdate"]:""); // 开始时间
    	$enddate = isset($_GET['enddate']) && !empty($_GET['enddate'])?strtotime($_GET['enddate']):
    		(isset($_COOKIE['search']["enddate"]) && !empty($_COOKIE['search']["enddate"])?$_COOKIE['search']["enddate"]:"");  //结束时间
    	if(!empty($startdate) && !empty($enddate)){
    		$where['date'] = $startdate.'>= startDate and endDate >='.$enddate;
    	}elseif(!empty($startdate) && empty($enddate)){
    		$where['date'] = $startdate.'>= startDate and endDate >='.$startdate;
    	}elseif(empty($startdate) && !empty($enddate)){
    		$where['date'] = $enddate.'>= startDate and endDate >='.$enddate;
    	}else{
    		$where['date'] = '';
    	}
    	return $where;
    }
    /**
     * 通过GET参数获取要查询的房屋ID (要将各个条件取得房屋ID进行整合)
     */
    private function get_hid($gets,$cid){
    	$fx =  $this->get_hid_fx($gets['fx'],$cid); //通过房型 (样式)获得的后屋ID
    	$pt = $this->get_hid_pt($gets);
    	$ts = $this->get_hid_ts($gets['ts'],$cid);
    	if(empty($fx)){
    		$fx = array();
    	}
    	if(empty($pt)){
    		$pt = array();
    	}
    	if(empty($ts)){
    		$ts = array();
    	}
		$hids = array_intersect($fx,$pt,$ts); //合并数组(取有重复的值)
    	return implode(',', $hids);
    }
    private function get_hid_ts($tese,$cid){
    	$db = M("house_tese_rel");
    	$where_tsid = $tese?" tsid = '".$tese."'":'';
    	$rows = $db->where(" cid = '".$cid."'")->where($where_tsid)->all();
    	if(empty($rows)){
    		$rows = array();
    	}
    	$hid = $this->fromArrToHid($rows);
    	if(empty($hid)){
    		$hid = array();
    	}
    	$hids = array_unique($hid);
    	return $hids;
    }
    /**
     * 通过get参数获得房型(该样式的房屋id)
     */
    private function get_hid_fx($fx,$cid){
    	$db = M("house_yangshi_rel");
    	$where_yid = $fx?" yid = '".$fx."'":'';
    	$rows = $db->where(" cid = '".$cid."'")->where($where_yid)->all();
    	$hid = $this->fromArrToHid($rows);
    	return $hid;
    }
    private function get_hid_pt($gets){
    	$pt = $gets['pt']; //一个字符串 -
    	$pt = str_replace('-', ',', $pt); //将其中的  - 替换成为 ,
    	$pt_arr = explode(',',$pt);
    	foreach($pt_arr as $k=>$v){
    		if($v ==0){
    			unset($pt_arr[$k]);
    		}
    	}
    	$count = count($pt_arr);
    	$db = M("house_sheshi_rel");
    	$where_hsid = $pt?" hsid in (".$pt.")":'';
    	$rows = $db->where($where_hsid)->all();
    	//$this->get_hid_pt_each($pt_arr, $rows);
    	$hid = $this->fromArrToHid($rows);
    	$hid_count = array_count_values($hid); //统计数组中所有的值出现的次数
    	foreach($hid_count as $k=>$v){
    		if($v >= $count){
    			$hids[] = $k;
    		}
    	}
    	if(empty($hids)){
    		$hids = array();
    	}
    	return $hids;
    }
    /**
     * 循环数组 取出需要的hid
     * @param unknown_type $rows
     * @return multitype:unknown
     */
    private function fromArrToHid($rows){
    	if(empty($rows)){
    		$rows = array();
    	}
    	$hid = array();
    	foreach($rows as $k=>$v){
    		$hid[] = $v['hid'];
    	}
    	return $hid;
    }
    /**
     * 根据GET 参数 来获取 价格参数
     * @param get中的价格数字 $price
     * @return string sql语句where
     */
    private function get_price_where($price){
    	$where_price = '';
    	if($price == 0){
    		$where_price = '';
    	}elseif($price == 1){
    		$where_price = ' price < 200';
    	}elseif($price == 2){
    		$where_price = ' price between 200 and 300 ';
    	}elseif($price == 3){
    		$where_price = ' price between 300 and 500 ';
    	}elseif($price == 4){
    		$where_price = ' price between 500 and 800 ';
    	}elseif($price == 5){
    		$where_price = ' price >800 ';
    	}
    	return $where_price;
    }
    /**
     * 获得当前房屋下面的所有景区
     * @param $cid
     */
    private function get_jingqu_view($cid){
    	$db = M("jingqu");
    	$rows = $db->field("jid,jname,cid,status")->where(" cid = '".$cid."'")->where(" status = 1")->all();
    	$this->assign("jingqu",$rows);
    	return $rows;
    }
    
    /**
     * 分配当前城市的小区信息到模板
     * @param number $cid
     */
    private function get_xq_view($cid){
    	$db = M("xiaoqu");
    	$xiaoqu = $db->field("qid,cid,qname,status")->where(" cid = '".$cid."'")->where(" status = 1")->all();
    	$this->assign("xiaoqu",$xiaoqu);
    }
    private function get_ts_view($cid){
    	$db = M("house_tese_rel");
    	$rows = $db->where(" cid = '".$cid."'")->all();
    	if(empty($rows)){
    		$rows = array();
    		$this->assign("tese",$rows);
    	}else{
	    	$hid = array();
	    	foreach($rows as $k=>$v){
	    		$tese[] = $v['tsid'];
	    	}
	    	if(empty($tese)){
	    		$tese = array();
	    	}
	    	$result = array_unique($tese);
	    	$result = implode(',',$result);
	    	$where_tsid = $result?" tsid in (".$result.") ":'';
	    	$tsname = $db->table("house_tese")->where($where_tsid)->all();
	    	$this->assign("tese",$tsname);
    	}
    }
    /**
     * 处理搜索页面的GET参数
     * @param Array $_GET
     */
    private function do_get($_GET){
    	$gets = array();
    	$get = explode('-', $_GET['s']);
    	$gets['jq'] = isset($get[0])?(int)$get[0]:0;  //景区
    	$gets['fx'] = isset($get[1])?(int)$get[1]:0;  //房型 (样式)
    	$gets['p'] = isset($get[2])?(int)$get[2]:0;  //价格
    	$gets['jg'] = isset($get[3])?(int)$get[3]:0;  // 景观
    	$gets['xq'] = isset($get[4])?(int)$get[4]:0;  //小区
    	$gets['ts'] = isset($get[5])?(int)$get[5]:0;  //特色
    	$pt = array(); //配置(设施)
    	if(strpos($_GET['pt'],'-')){
    		$gets['pt'] = $_GET['pt']; //这里是 查询数据库需要
    		$pt = explode('-',$_GET['pt']);
    	}else{
    		$gets['pt'] = $_GET['pt'];
    		$pt = array(0=>$_GET['pt']);
    	}
    	//下面的条件
    	$order_arr = array('hot','area','price'); //定义根据 价格，热度，面积的排序
    	if(isset($_GET['order']) && in_array($_GET['order'],$order_arr)){ 
    		$order = $_GET['order'];
    	}else{
    		$order = 'hot';
    	}
    	$gets['order'] = $order;
    	$orderstr = '/order/'.$order;
    	$this->assign("order",$order);
    	$this->assign("orderstr",$orderstr);
    	$this->assign('pt', $pt); //array
    	$this->assign('ptstr',$_GET['pt']);
    	$this->assign('gets', $gets);
    	return $gets;
    }
    /**
    * 发送用户已经使选中的选项到模板
    * @param Array $gets  传递进来的已经处理好的get参数数组
    * 
    */
    private function do_options($gets){
    	$db = M();
    	$result = array();
    	if(!empty($gets['jq'])){ //景区
    		$result['jq'] = $db->table("jingqu")->field("jid|id,jname|value")->where(" jid = '".$gets['jq']."'")->find();
    		$result['jq']['name'] = '景区';
    		$result['jq']['op'] = "0-{$gets['fx']}-{$gets['p']}-{$gets['jg']}-{$gets['xq']}-{$gets['ts']}";
    	}
    	if(!empty($gets['fx'])){ //房型（样式）
    		$result['fx'] = $db->table("house_yangshi")->field("yid|id,yname|value")->where(" yid = '".$gets['fx']."'")->find();
    		$result['fx']['name'] = '房型';
    		$result['fx']['op'] = "{$gets['jq']}-0-{$gets['p']}-{$gets['jg']}-{$gets['xq']}-{$gets['ts']}";
    	}
    	if(!empty($gets['p'])){ //价格
    		if($gets['p'] == 1){
    			$result['p'] = array('value'=>'￥200以下','id'=>1);
    		}elseif($gets['p'] == 2){
    			$result['p'] = array('value'=>'￥200-300','id'=>2);
    		}elseif($gets['p'] == 3){
    			$result['p'] = array('value'=>'￥300-500','id'=>3);
    		}elseif($gets['p'] == 4){
    			$result['p'] = array('value'=>'￥500-800','id'=>4);
    		}elseif($gets['p'] == 5){
    			$result['p'] = array('value'=>'￥800以上','id'=>5);
    		}
    		$result['p']['name'] = '价格';
    		$result['p']['op'] = "{$gets['jq']}-{$gets['fx']}-0-{$gets['jg']}-{$gets['xq']}-{$gets['ts']}";
    	}
    	if(!empty($gets['jg'])){ //景观
    		$result['jg'] = $db->table("house_jingguan")->field("jgid|id,jgname|value")->where(" jgid = '".$gets['jg']."'")->find();
    		$result['jg']['name'] = '景观';
    		$result['jg']['op'] = "{$gets['jq']}-{$gets['fx']}-{$gets['p']}-0-{$gets['xq']}-{$gets['ts']}";
    	}
    	if(!empty($gets['xq'])){ //小区
    		$result['xq'] = $db->table("xiaoqu")->field("qid|id,qname|value")->where(" qid = '".$gets['xq']."'")->find();
    		$result['xq']['name'] = '小区';
    		$result['xq']['op'] = "{$gets['jq']}-{$gets['fx']}-{$gets['p']}-{$gets['jg']}-0-{$gets['ts']}";
    	}
    	if(!empty($gets['ts'])){  //特色
    		$result['ts'] = $db->table("house_tese")->field("tsid|id,tsname|value")->where(" tsid = '".$gets['ts']."'")->find();
    		$result['ts']['name'] = '特色';
    		$result['ts']['op'] = "{$gets['jq']}-{$gets['fx']}-{$gets['p']}-{$gets['jg']}-{$gets['xq']}-0";
    	}
    	return $result;
    }
    private function get_jingguan_view($cid){
    	$db = M("house");
    	$house_rows = $db->field("jingguan|jgid")->where(" status = 1")->where("cid = '".$cid."'")->all();
    	if(!$house_rows){
    		$house_rows = array();
    	}
    	$jgids = houseControl::getArrFieldByKey($house_rows,'jgid');
    	$jgids = $jgids?$jgids:array();
    	$jgids_str = implode(',',$jgids);
    	$where_jg = $jgids_str?" jgid in (".$jgids_str.")":false;
    	if(empty($where_jg)){
    		$jingguan = array();
    	}else{
    		$jingguan = $db->table("house_jingguan")->where($where_jg)->all();
    	}
    	
    	$this->assign("jingguan",$jingguan);
    	return $jingguan;
    }
    /**
     * 获得房屋样式(获得某个城市的所有的房屋样式)
     * @param number $cid
     */
    private function get_yangshi_view($cid){
    	$db = V('house_yangshi');
    	$db->view = array(
    			'house_yangshi_rel'=>array(
    					'join_type'=>'house_yangshi_rel',
    					'on'=>'house_yangshi.yid = house_yangshi_rel.yid',
    				),
    		);
    	$yangshi = $db->where(" cid = '".$cid."'")->all();
    	$yangshi_re = array();
    	if(empty($yangshi)){
    		$yangshi = array();
    	}
    	$yangshi_re = $this->assoc_unique_key($yangshi, 'yname');
    	$this->assign('yangshi', $yangshi_re);
    	return $yangshi_re;
    }
    /**
     * 获得所有的城市信息(像页面发送城市信息)(用于搜索页面选择城市搜索)
     */
    public function get_citys_view(){
    	$db = M('citys');
    	$citys = $db->field('cid,enname,cname,ishot')->where(" status = 1")->all();
    	$this->assign('citys',$citys); //向模版视图分配数据
    	$rand_city = array();
    	$rand_city = array_rand($citys);
    	$this->assign("rand_city",$citys[$rand_city]);
    }
    private function get_sheshi_view(){
    	$db = M('house_sheshi');
    	$sheshi = $db->all();
    	$this->assign("sheshi",$sheshi);
    }
    /**
     * 设置搜索COOKIE 将要搜索的城市  开始时间 结束时间 全部记录到cookie里面去
     */
    private function setCookie($_GET){
    	if(isset($_GET['cid_']) && !empty($_GET['cid_']) && $_GET['cid'] == $_GET['cid_']){
    		setcookie('search[guestCount]','');
    		setcookie('search[woshiCount]','');
    		setcookie('search[woshiCount]','');
    		setcookie('search[addBed]','');
    		setcookie('search[cid]',$_GET['cid'],time()+60*60*24*30); //设置城市ID
    		if(!isset($_GET['startdate']) || empty($_GET['startdate'])){
    			$_GET['startdate'] = '';
    		}
    		if(!isset($_GET['enddate']) || empty($_GET['enddate'])){
    			$_GET['enddate'] = '';
    		}
    		setcookie('search[startdate]',strtotime($_GET['startdate']),time()+60*60*24*30); //设置开始时间
    		setcookie('search[enddate]',strtotime($_GET['enddate']),time()+60*60*24*30);  //结束时间
    		isset($_GET['guestCount'])?setcookie('search[guestCount]',$_GET['guestCount'],time()+60*60*24*30):''; // 人数
    		isset($_GET['chuangCount'])?setcookie('search[chuangCount]',$_GET['chuangCount'],time()+60*60*24*30):''; //床数
    		isset($_GET['woshiCount'])?setcookie('search[woshiCount]',$_GET['woshiCount'],time()+60*60*24*30):''; //卧室数
    		isset($_GET['addBed'])?setcookie('search[addBed]',$_GET['addBed'],time()+60*60*24*30):''; // 是否加床
    	}
    }
    /**
     * 从cookie中读取有关的搜索的信息  分配到模版页面(其中GET参数的优先级要高)
     */
    private function get_cookieAndget($_GET){
    	$cid = (isset($_GET['cid']) && !empty($_GET['cid']))?$_GET['cid']:(isset($_COOKIE['tiaojian']['cid'])?$_COOKIE['tiaojian']['cid']:'');
    	$startdate = (isset($_GET['startdate']) && !empty($_GET['startdate']))?strtotime($_GET['startdate']):(isset($_COOKIE['tiaojian']['startdate'])?$_COOKIE['tiaojian']['startdate']:'');
    	$enddate = (isset($_GET['enddate']) && !empty($_GET['enddate']))?strtotime($_GET['enddate']):(isset($_COOKIE['tiaojian']['enddate'])?$_COOKIE['tiaojian']['enddate']:'');
    	isset($_GET['guestCount']) && !empty($_GET['guestCount'])?$guestCount = $_GET['guestCount']:(isset($_COOKIE['tiaojian']['guestCount'])?$_COOKIE['tiaojian']['guestCount']:'');
    	isset($_GET['chuangCount']) && !empty($_GET['chuangCount'])?$chuangCount = $_GET['chuangCount']:(isset($_COOKIE['tiaojian']['chuangCount'])?$_COOKIE['tiaojian']['chuangCount']:'');
    	isset($_GET['woshiCount']) && !empty($_GET['woshiCount'])?$woshiCount = $_GET['woshiCount']:(isset($_COOKIE['tiaojian']['woshiCount'])?$_COOKIE['tiaojian']['woshiCount']:'');
    	isset($_GET['addBed']) && !empty($_GET['addBed'])?$addBed = $_GET['addBed']:(isset($_COOKIE['tiaojian']['addBed'])?$_COOKIE['tiaojian']['addBed']:'');
    	$search_cookie = array();
    	$search_cookie['cid'] = $cid;
    	$search_cookie['startdate'] = $startdate;
    	$search_cookie['enddate'] = $enddate;
    	isset($guestCount)?$search_cookie['guestCount'] = $guestCount:'';
    	isset($chuangCount)?$search_cookie['chuangCount'] = $chuangCount:'';
    	isset($woshiCount)?$search_cookie['woshiCount'] =$woshiCount:'';
    	isset($addBed)?$search_cookie['addBed'] =$addBed:'';
    	if(isset($guestCount)){
    		$num = $guestCount;
    	}elseif(isset($chuangCount)){
    		$num = $chuangCount;
    	}elseif(isset($woshiCount)){
    		$num = $woshiCount;
    	}else{
    		$num = 0;
    	}
    	$search_cookie['num'] = $num;
    	$db = M('citys');
    	$city = $db->find($cid);
    	$search_cookie['cname'] = $city['cname'];
    	$this->assign('search_cookie',$search_cookie);
    }
    /**
     * 二维数组去重(根据键)
     * @param Array $arr
     * @param 根据去重的键 $key
     * @return unknown
     */
    private function assoc_unique_key($arr, $key) {
    	if(empty($arr)) return $arr;    		
	    $tmp_arr = array();   
	    foreach($arr as $k => $v) {   
	        if(in_array($v[$key], $tmp_arr)) {   
	            unset($arr[$k]);   
	        } else {   
	            $tmp_arr[] = $v[$key];   
	        }   
	    }   
	    sort($arr);
	    return $arr;   
	}
}